// utils/apiService.js  
const API_URL = 'https://api.302.ai/v1/chat/completions';  
const API_KEY = 'sk-z7jn9DgYAZ8I61RQZqMo8DLTO4VtmNt9ebkBEcVroY0UXc3v'; // 替换为你的实际 API 密钥  

export const chatWithAI = (userMessage) => {  
    return new Promise((resolve, reject) => {  
        uni.request({  
            url: API_URL,  
            method: 'POST',  
            header: {  
                'Authorization': `Bearer ${API_KEY}`,  
                'Content-Type': 'application/json',  
            },  
            data: {  
                messages: [  
                    {  
                        role: 'user',  
                        content: userMessage,  
                    },  
                ], 
                model: 'gpt-4o-mini',  
				// stream:true,
            },  
            success: (res) => {  
                if (res.statusCode === 200) {  
                    resolve(res.data); 
					// const stream = res.data;
					// //处理收到的数据块
					// stream.on('data',(chunk)=>{
					// 	console.log("Receice data:",chunk);
					// })
					// //数据接收完毕
					// stream.on('end',()=>{
					// 	console.log("接收完毕");
					// })
                } else {  
                    reject(`Error: ${res.statusCode}`);  
                }  
            },  
            fail: (error) => {  
                reject(`API Request Failed: ${error}`);  
            },  
        });  
    });  
};

export default chatWithAI